import { tool } from '../../tool/UITool';
import { Buff } from '../../object/Buff';

const { ccclass, property } = cc._decorator;

@ccclass
export default class BuffUI extends cc.Component {
  @property(cc.Node)
  highLight: cc.Node = null;

  @property(cc.Sprite)
  itemImg: cc.Sprite = null;

  @property(cc.Label)
  itemNum: cc.Label = null;

  // LIFE-CYCLE CALLBACKS:

  start() {}

  public renderBuff(buff: Buff): void {
    this.renderIcon(buff);
    this.renderNum(buff);
  }

  public renderIcon(buff: Buff): void {
    if (buff.icon) {
      this.itemImg.node.active = true;
      let imgSource: string = `images/buff_icon/${buff.icon}`;
      tool.setSource(this.itemImg, imgSource);
    } else {
      this.itemImg.node.active = false;
    }
  }

  public oldCeng: number;
  public renderNum(buff: Buff): void {
    this.itemNum.node.scale = 1;
    if (buff.ceng == 0) {
      this.itemNum.node.active = false;
    } else {
      this.itemNum.node.active = true;
      this.itemNum.string = `${buff.ceng}`;
      if (buff.ceng > this.oldCeng) {
        this.itemNum.node.stopAllActions();
        let action: cc.Action = cc.sequence(cc.scaleTo(0.2, 2), cc.scaleTo(0.2, 1));
        this.itemNum.node.runAction(action);
      }
    }
    this.oldCeng = buff.ceng;
  }

  // update (dt) {}
}
